package com.andy.buyu.server.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.andy.buyu.server.BuyuApplication;
import com.andy.buyu.server.base.HttpClientUtil;
import com.andy.buyu.server.base.PageUtil;
import com.andy.buyu.server.base.PageVO;
import com.andy.buyu.server.base.RespVO;
import com.andy.buyu.server.base.form.FormField;
import com.andy.buyu.server.base.table.PageStructure;
import com.andy.buyu.server.base.table.TableHeader;
import com.andy.buyu.server.base.table.Tool;
import com.andy.buyu.server.dao.DaoUtils;
import com.andy.buyu.server.dao.vo.Where;
import com.andy.buyu.server.entity.ConfCommon;
import com.andy.buyu.server.entity.ConfScript;
import com.andy.buyu.server.game.GameConf;
import com.andy.buyu.server.utils.GoConstant;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("admin/confScript")
public class ConfScriptController extends BaseController{

	/**
	 * 场景配置列表：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="list")
	public ModelAndView list() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{ "script_id","next_id", "fish_id","show_time","out_time","direc","trai"});
		tableHeader.setTexts(new String[]{"剧本ID", "下个剧本ID","鱼类ID","出现时间","消亡时间","初始朝向","行驶轨迹"});
		
		Tool tool = new Tool();
		tool.setUrls("admin/confScript/add","admin/confScript/edit","admin/confScript/delete","admin/confScript/refreshCache");
		tool.setTexts("新增","编辑","删除","配置生效");
		tool.setTypes("add","edit","del","confirm");
		
		PageStructure data = PageUtil.createTablePageStructure("admin/confScript/listData","id", tableHeader,tool,null);
		data.setExportTable(ConfScript.table());
		return createMV("tableList","子剧本配置", Collections.singletonMap("formStruct", data));
	}
	
	/**
	 * 场景配置：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="listData")
	public RespVO listData(Integer page,Integer limit) throws Exception {
		PageVO result = DaoUtils.getPageForMap(ConfScript.table(), null,"script_id",page,limit);
		
        return RespVO.createSuccessJsonResonse(result);
	}
	
	/**
	 * 编辑
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@RequestMapping(value="add")
	public ModelAndView add() throws Exception {
		List<FormField> formFieldList = new ArrayList<>();
		formFieldList.add(FormField.builder().name("scriptId").text("剧本ID").build());
		formFieldList.add(FormField.builder().name("nextId").text("下个剧本ID").build());
		formFieldList.add(FormField.builder().name("fishId").text("鱼类ID").build());
		formFieldList.add(FormField.builder().name("showTime").text("出现时间").build());
		formFieldList.add(FormField.builder().name("outTime").text("消亡时间").build());
		formFieldList.add(FormField.builder().name("direc").text("初始朝向").build());
		formFieldList.add(FormField.builder().name("trai").text("行驶轨迹").build());
		
		Map<String, Object> data = PageUtil.createFormPageStructure("admin/confScript/save", formFieldList);
		
		return createCustMV("window/add",data);
	}
	
	/**
	 * 新增
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public RespVO save(ConfScript confScript) throws Exception {
		DaoUtils.insert(confScript);
		
		return RespVO.createSuccessJsonResonse("配置已保存");
	}
	
	/**
	 * 编辑
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@RequestMapping(value="edit")
	public ModelAndView edit(Long id) throws Exception {
		List<FormField> formFieldList = new ArrayList<>();
		formFieldList.add(FormField.builder().name("scriptId").text("剧本ID").type("span").build());
		formFieldList.add(FormField.builder().name("nextId").text("下个剧本ID").build());
		formFieldList.add(FormField.builder().name("fishId").text("鱼类ID").build());
		formFieldList.add(FormField.builder().name("showTime").text("出现时间").build());
		formFieldList.add(FormField.builder().name("outTime").text("消亡时间").build());
		formFieldList.add(FormField.builder().name("direc").text("初始朝向").build());
		formFieldList.add(FormField.builder().name("trai").text("行驶轨迹").build());
		ConfScript confTitle = DaoUtils.getOne(ConfScript.class,Where.eq("id", id));
		
		Map<String, Object> data = PageUtil.createFormPageStructure("admin/confScript/update", formFieldList,confTitle);
		
		return createCustMV("window/add",data);
	}
	
	/**
	 * 更新
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public RespVO update(ConfScript confScript) throws Exception {
		DaoUtils.update(confScript);
		
		return RespVO.createSuccessJsonResonse("配置已保存");
	}
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public RespVO delete(Long id) throws Exception {
		DaoUtils.delete(ConfScript.table(), id);
		return RespVO.createSuccessJsonResonse("删除成功");
	}
	
	/**
	 * 配置生效
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="refreshCache")
	public RespVO refreshCache() throws Exception {
		if(BuyuApplication.gameServerEnable()){
			GameConf.initScripts();
		}else{
			ConfCommon conf = DaoUtils.getOne(ConfCommon.class, Where.eq("nid", "game_server_url"));
			ConfCommon secKey = DaoUtils.getOne(ConfCommon.class, Where.eq("nid", "commu_seckey"));
			Map<String,Object> params = Maps.newHashMap();
			params.put("seckey", secKey.getValue());
			HttpClientUtil.post(conf.getValue()+"admin/cmd/refreshCache/"+GoConstant.CMD_4, params, null);
		}
        return RespVO.createSuccessJsonResonse("配置已生效，需重新登录游戏验证");
	}
}
